//+---------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation, 2008
//
// File: DeliveryServiceConfig.cs
//
// Contents:
//     Provides easy access to the configuration parameters in the [assembly].config file.
//
// Created by:
//     Adam Wardynski (adamwa) Apr 8, 2009
//
//----------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Reputation.Services
{
    /// <summary>
    /// Provides easy access to the configuration parameters in the [assembly].config file.
    /// </summary>
    internal class DeliveryServiceConfig : ConfigFileBase
    {
        public const string DataProviderTypeDefault = "Microsoft.Reputation.Services.DatabaseDataProvider";
        public const string CacheDataProviderTypeDefault = "Microsoft.Reputation.Services.CacheDataProvider";
        public const string CacheProviderTypeDefault = "Microsoft.Reputation.Services.PNPCacheProvider";
        public const string DatabaseAccessTypeDefault = "Microsoft.Reputation.Services.DatabaseAccessDS";
        public const string StaticCacheFilenameDefault = "StaticCache.bin";
        public const string AllowedLookupUrlIdDefault = "10f54ef1-f5bd-4fd3-b9c7-15eb086133bb";

        public const bool ApiLoggingTurnedOffDefault = false;
        public const bool PerformanceCounterDisabledDefault = false;
        public const bool EventLoggerDisabledDefault = false;
        public const bool EventLoggerThrottleDisabledDefault = false;
        public const bool ReputationCacheEnabledDefault = true;
        
        public const int DelayCommunicationMinutesDefault = 0;
        public const int CacheExpirationMinutesDefault = 60 * 24; // 1 day

        public const int MaxLookupRequestEntriesCountDefault = 250;
        public const int MaxListingRequestEntriesCountDefault = 5;
        public const int ListingPageSizeDefault = 500;
        
        public const int EventLogIntervalInMillisecondsDefault = 60 * 60 * 1000; // 1 hour
        public const int LookupCachingExpirationInSecondsDefault = 10 * 60; // 10 minutes
        public const int NoMatchExpirationMinutesDefault = 20; // minutes
        public const int MaximumOverridesPerPacketDefault = 50;
        public const int OverrideQueryIntervalInSecondsDefault = 60;
        public const int ReputationExpirationDefault = 10080;

        public const uint OverrideResponseSequenceNumber = uint.MaxValue;

        private const string DataProviderTypeKey = "dataProviderType";
        private const string DatabaseAccessTypeKey = "databaseAccessType";
        private const string ApiLoggingTurnedOffKey = "apiLoggingTurnedOff";
        private const string StaticCacheFilenameKey = "staticCacheFilename";

        internal const string CacheProviderTypeKey = "cacheProviderType";
        private const string CacheExpirationMinutesKey = "cacheExpirationMinutes";
        public const string DelayCommunicationMinutesKey = "delayCommunicationMinutes";

        public const string MaxLookupRequestEntriesCountKey = "maxLookupRequestEntriesCount";
        public const string MaxListingRequestEntriesCountKey = "maxListingRequestEntriesCount";
        public const string ListingPageSizeKey = "listingPageSize";

        public const string PerformanceCounterDisabledKey = "performanceCounterDisabled";
        public const string EventLoggerDisabledKey = "eventLoggerDisabled";
        public const string EventLoggerThrottleDisabledKey = "eventLoggerThrottleDisabled";
        public const string EventLogIntervalInMillisecondsKey = "EventLogIntervalInMilliseconds";
        public const string LookupCachingExpirationInSecondsKey = "LookupCachingExpirationInSeconds";

        /// This key turns on and off the ability of outside machines to call the 
        /// LookupCategoriesForUrl method of the Delivery Service.  If set to true, 
        /// machines with a specific ClientId in their ClientIdToken will be able to 
        /// call the API
        public const string LookupCategoriesForUrlApiEnabledKey = "lookupCategoriesForUrlApiEnabled";
        public const string AllowedLookupUrlIdKey = "lookupCategoriesForUrlApiAllowedId";

        public const string NoMatchExpirationMinutesKey = "noMatchCacheExpirationMinutes";

        /// <summary>
        /// This key is to set the maximum number of overrides that we will add on to a 
        /// LookupRequest that is sent back to the client.
        /// </summary>
        public const string MaximumOverridesPerPacketKey = "maximumOverridesPerPacket";

        /// <summary>
        /// Value to determine how often to query the database for override triggers.
        /// </summary>
        public const string OverrideQueryIntervalInSecondsKey = "overrideQueryIntervalInSeconds";

        /// <summary>
        /// Value to determine how long a ReputationObject will be cached by the client.
        /// </summary>
        public const string ReputationExpirationKey = "reputationObjectExperationInMinutes";

        public const string ReputationCacheEnabledKey = "reputationCacheEnabled";

        public static uint DelayCommunicationMinutes
        {
            get
            {
                return (uint)GetParameterValue(
                    DelayCommunicationMinutesKey,
                    DelayCommunicationMinutesDefault,
                    0,
                    int.MaxValue);
            }
        }

        public static string DataProviderType
        {
            get
            {
                return GetParameterValue(DataProviderTypeKey, CacheDataProviderTypeDefault);
            }
        }

        public static string CacheProviderType
        {
            get
            {
                return GetParameterValue(CacheProviderTypeKey, CacheProviderTypeDefault);
            }
        }

        public static int CacheExpirationMinutes
        {
            get
            {
                return GetParameterValue(
                    CacheExpirationMinutesKey,
                    CacheExpirationMinutesDefault,
                    0,
                    int.MaxValue);
            }
        }

        public static string StaticCacheFilename
        {
            get
            {
                return GetParameterValue(StaticCacheFilenameKey, StaticCacheFilenameDefault);
            }
        }

        public static string DatabaseAccessType
        {
            get
            {
                return GetParameterValue(DatabaseAccessTypeKey, DatabaseAccessTypeDefault);
            }
        }

        public static bool ApiLoggingTurnedOff
        {
            get
            {
                return GetParameterValue(ApiLoggingTurnedOffKey, ApiLoggingTurnedOffDefault);
            }
        }

        public static int MaxLookupRequestEntriesCount
        {
            get
            {
                return GetParameterValue(
                    MaxLookupRequestEntriesCountKey,
                    MaxLookupRequestEntriesCountDefault,
                    1,
                    int.MaxValue);
            }
        }

        public static int MaxListingRequestEntriesCount
        {
            get
            {
                return GetParameterValue(
                    MaxListingRequestEntriesCountKey,
                    MaxListingRequestEntriesCountDefault,
                    1,
                    int.MaxValue);
            }
        }

        public static int ListingPageSize
        {
            get
            {
                return GetParameterValue(
                    ListingPageSizeKey,
                    ListingPageSizeDefault,
                    1,
                    int.MaxValue);
            }
        }

        public static bool PerformanceCounterDisabled
        {
            get
            {
                return GetParameterValue(PerformanceCounterDisabledKey, PerformanceCounterDisabledDefault);
            }
        }

        public static bool EventLoggerDisabled
        {
            get
            {
                return GetParameterValue(EventLoggerDisabledKey, EventLoggerDisabledDefault);
            }
        }

        public static bool EventLoggerThrottleDisabled
        {
            get
            {
                return GetParameterValue(EventLoggerThrottleDisabledKey, EventLoggerThrottleDisabledDefault);
            }
        }

        public static int EventLogIntervalInMilliseconds
        {
            get
            {
                return GetParameterValue(
                    EventLogIntervalInMillisecondsKey,
                    EventLogIntervalInMillisecondsDefault,
                    0,
                    int.MaxValue);
            }
        }

        public static int LookupCachingExpirationInSeconds
        {
            get
            {
                return GetParameterValue(
                    LookupCachingExpirationInSecondsKey,
                    LookupCachingExpirationInSecondsDefault,
                    0,
                    int.MaxValue);
            }
        }

        public static bool LookupCategoriesForUrlApiEnabled 
        {
            get
            {
                // The true as the second parameter is the default value for this
                // parameter.  We want it to be enabled all the time and be explicitly
                // disabled in the web.config
                return GetParameterValue(LookupCategoriesForUrlApiEnabledKey, true);
            }
        }

        public static string AllowedLookupUrlId
        {
            get
            {
                return GetParameterValue(AllowedLookupUrlIdKey, AllowedLookupUrlIdDefault);
            }
        }

        public static int NoMatchExpirationMinutes
        {
            get
            {
                return (int)GetParameterValue(NoMatchExpirationMinutesKey, NoMatchExpirationMinutesDefault, 0, int.MaxValue);
            }
        }

        public static int MaxOverridesPerPacket
        {
            get
            {
                return GetParameterValue(MaximumOverridesPerPacketKey, MaximumOverridesPerPacketDefault, 0, int.MaxValue);
            }
        }

        public static int OverrideQueryIntervalInSeconds
        {
            get
            {
                return GetParameterValue(OverrideQueryIntervalInSecondsKey, OverrideQueryIntervalInSecondsDefault, 0, int.MaxValue);
            }
        }

        public static uint ReputationExpiration
        {
            get
            {
                return (uint)GetParameterValue(ReputationExpirationKey, ReputationExpirationDefault, 0, int.MaxValue);
            }
        }
        
        public static bool ReputationCacheEnabled
        {
            get
            {
                return GetParameterValue(ReputationCacheEnabledKey, ReputationCacheEnabledDefault);
            }
        }
    }
}
